package com.example.hospitalManage.controller.admin.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.hospitalManage.entity.Department;
import com.example.hospitalManage.service.DepartmentService;
import com.example.hospitalManage.utils.Response;
import com.example.hospitalManage.utils.ResponseResult;
import com.example.hospitalManage.vo.DepartmentQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Description:
 * Param:
 * return:
 * Author: 彭雨胜
 * Data: 2022/12/30
 */
@RestController
@RequestMapping("/admin")
@Api(value = "v1",tags = "医院科室相关接口")
@CrossOrigin
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    //定义日志对象
    public final static Logger log = LoggerFactory.getLogger(DepartmentController.class);

    // 查询所有科室信息
    @ApiOperation(value = "查询所有科室信息")
    @GetMapping("department")
    public ResponseResult<List<Department>> findAllDepartment() {
        List<Department> departmentList = departmentService.list();
        return Response.createOkResp(departmentList);
    }

    // 根据id查询科室信息
    @ApiOperation(value = "根据id查询科室信息")
    @GetMapping("department/{id}")
    public ResponseResult showDepartmentById(@PathVariable Long id) {
        Department department = departmentService.getById(id);
        return Response.createOkResp(department);
    }

    // 上传科室接口
    @ApiOperation(value = "上传科室接口")
    @PostMapping("department")
    public ResponseResult saveDepartment(@RequestBody Department department) {
        department.setIsDeleted(0);

        // 调用service的save方法
        boolean save = departmentService.save(department);
        if(save) {
            return Response.createOkResp();
        } else {
            return Response.createFailResp();
        }
    }

    // 修改科室信息接口
    @PutMapping("department")
    @ApiOperation(value = "修改科室信息接口")
    public ResponseResult updateDepartment(@RequestBody Department department) {
        boolean flag = departmentService.updateById(department);
        if(flag) {
            return Response.createOkResp();
        } else {
            return Response.createFailResp();
        }
    }

    // 删除科室接口
    @ApiOperation(value = "根据id删除科室")
    @DeleteMapping("department/{id}")
    public ResponseResult removeDepartmentById(@PathVariable Long id) {
        boolean flag = departmentService.removeById(id);
        if(flag) {
            return Response.createOkResp();
        } else {
            return Response.createFailResp("删除科室操作失败");
        }
    }

    // 条件查询带分页
    @ApiOperation(value = "条件分页查询")
    @PostMapping("department/{current}/{limit}")
    public ResponseResult findPageHospSet(@PathVariable long current,
                                          @PathVariable long limit,
                                          @RequestBody(required = false) DepartmentQueryVo departmentQueryVo) {
        //创建page对象，传递当前页，每页记录数
        Page<Department> page = new Page<>(current, limit);
        //构建条件
        QueryWrapper<Department> wrapper = new QueryWrapper<>();
        String hoscode = departmentQueryVo.getHoscode();  // 医院编号
        String depcode = departmentQueryVo.getDepcode();  // 科室编号
        String depname = departmentQueryVo.getDepname();  // 科室名称
        String bigname = departmentQueryVo.getBigname();  // 大科室名称

        if(!StringUtils.isEmpty(depname)) {
            wrapper.like("depname",departmentQueryVo.getDepname());
        }
        if(!StringUtils.isEmpty(bigname)) {
            wrapper.like("bigname",departmentQueryVo.getBigname());
        }
        if(!StringUtils.isEmpty(hoscode)) {
            wrapper.eq("hoscode",departmentQueryVo.getHoscode());
        }
        if(!StringUtils.isEmpty(depcode)) {
            wrapper.eq("depcode",departmentQueryVo.getDepcode());
        }

        //调用方法实现分页查询
        IPage<Department> pageDepartment = departmentService.page(page, wrapper);

        //返回结果
        return Response.createOkResp(pageDepartment);
    }
}
